package com.ruoyi.wxapidaily;

import cn.hutool.core.img.Img;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.core.ImgUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.wx.domain.ADaily;
import com.ruoyi.wx.domain.ALikes;
import com.ruoyi.wx.domain.AWxuser;
import com.ruoyi.wx.service.IADailyService;
import com.ruoyi.wx.service.IALikesService;
import com.ruoyi.wx.service.IAWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * author：pan
 * 微信小程序  对用户上传的日常进行 /保存 / 分页查询 / 图片上传通用接口
 *
 */

@RestController
@RequestMapping("/wxapi")
public class WxAdminUploadDailyController {

    @Autowired
    private IADailyService dailyService;

    @Autowired
    private IAWxuserService wxuserService;

    @Autowired
    private ImgUtils imgUtils;

    @Autowired
    private ServerConfig serverConfig;


    /**
     * 管理员查询 未审核的日常
     * @param openid
     * @return
     */
    @GetMapping("/getAdminDailies")
    public AjaxResult getAdminDailies(@RequestParam(name = "openid") String openid){
        /**先通过openid来查询是否存在*/
        AWxuser wxuser = wxuserService.selectWxuserOpenID(openid);


        // 权限判断
        if(wxuser == null){
            return AjaxResult.error("用户不存在");
        }
        if(wxuser.getSituation().equals("禁用")){
            return AjaxResult.error("该用户已被禁用，请联系管理员！");
        }
        if(!wxuser.getIdentity().equals("管理员")){
            return AjaxResult.error("无权限修改！");
        }
        List<ADaily> aDailies = dailyService.getAdminDailies();


        return AjaxResult.success("查询未审核的日常成功", aDailies);
    }

    /**
     * 管理员进行日常审核   /  精选 /  图片压缩
     * @param object
     * @return
     */
    @PostMapping("/passDaily")
    public AjaxResult passDaily(@RequestBody JSONObject object){

        String openid = (String) object.get("openid");
        String imgCompress = (String) object.get("catCheck");   // 是否照片压缩
        String dailyChoice = (String) object.get("catChoice");  // 是否精选
        Integer dailyId = (Integer) object.get("dailyId");  // 是否精选

        if (StrUtil.isEmpty(openid)) {
            return AjaxResult.error("未获取到openid");
        }
        /**先通过openid来查询是否存在*/
        AWxuser wxuser = wxuserService.selectWxuserOpenID(openid);


        // 权限判断
        if(wxuser == null){
            return AjaxResult.error("用户不存在");
        }
        if(wxuser.getSituation().equals("禁用")){
            return AjaxResult.error("该用户已被禁用，请联系管理员！");
        }
        if(!wxuser.getIdentity().equals("管理员")){
            return AjaxResult.error("无权限修改！");
        }


        // 获取daily日常
        ADaily aDaily = dailyService.selectADailyById(dailyId.longValue());

        aDaily.setCatCheck("已通过");

        if(dailyChoice.equals("精选")){
            aDaily.setCatChoice("已通过");

        }

        dailyService.updateADaily(aDaily);  // 更新信息

        // 判断图片是否压缩
        if(!imgCompress.equals("图片压缩")){
            return AjaxResult.success("审核完成！");
        }

        // 图片操作-----------------------------------------------

        // 三个图片路径
        String catImg1 = aDaily.getCatImg1();
        String catImg2 = aDaily.getCatImg2();
        String catImg3 = aDaily.getCatImg3();

        // 判断是否为空
        if(StrUtil.isNotBlank(catImg1)){
            catImg1 = imgUtils.getFileImgUrl(catImg1);
            imgUtils.commpressPicForScale(catImg1, catImg1, 500, 0.8);
        }
        // 判断是否为空
        if(StrUtil.isNotBlank(catImg2)){
            catImg2 = imgUtils.getFileImgUrl(catImg2);
            imgUtils.commpressPicForScale(catImg2, catImg2, 500, 0.8);
        }
        // 判断是否为空
        if(StrUtil.isNotBlank(catImg3)){
            catImg3 = imgUtils.getFileImgUrl(catImg3);
            imgUtils.commpressPicForScale(catImg3, catImg3, 500, 0.8);
        }

        return AjaxResult.success("审核完成！");
    }


    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, @RequestHeader("header") String header) throws Exception
    {
        try
        {
            System.out.println("请求头："+header);
            // 判断header
            if(!header.equals("header")){
                return AjaxResult.error("请求头有误！");
            }

            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }


}
